from django.contrib.auth.models import AbstractUser
from django.db import models
from erp.utils.base_model import BaseModel


# ERP系统
class UserModel(AbstractUser, BaseModel):
    phone = models.CharField(verbose_name="手机号码", max_length=11, unique=True, blank=True, null=True)
    real_name = models.CharField(verbose_name="真实姓名", max_length=64, blank=True, null=True)
    # 用户所有的角色
    roles = models.ManyToManyField("RoleModel", db_table="t_user_roles", blank=True)
    # 重写groups字段并添加related_name
    groups = models.ManyToManyField(
        'auth.Group',
        related_name='groups',  # 自定义反向访问名称
        verbose_name='用户组',
        blank=True,
        help_text='用户所属的组',
        related_query_name='custom_user'
    )
    # 重写user_permissions字段并添加related_name
    user_permissions = models.ManyToManyField(
        'auth.Permission',
        related_name='user_permissions',  # 自定义反向访问名称
        verbose_name='用户权限',
        blank=True,
        help_text='用户拥有的权限',
        related_query_name='custom_user'
    )

    class Meta:
        db_table = 't_user'
        verbose_name = "系统用户表"
        verbose_name_plural = verbose_name

    def __str__(self):
        username = self.username or ""
        real_name = self.real_name or ""
        return f"{username}:{real_name}"


class PermissionsModel(BaseModel):
    """
    权限的模型类， 包含2个要素： 资源，操作
    """
    method_choices = (
        ('GET', '查询'),
        ('POST', '新增'),
        ('PUT', '修改'),
        ('DELETE', '删除'),
        ('PATCH', '改局部'),
    )
    """
    只要是choices参数的字段， 如果你想要获取对应的key和value， 默认获取的是key，
    如果需要获取value， 可以采用get_字段名_display()
    print(permission.method) 获取key
    print(permission.get_method_display()) 获取value
    """
    # method_choices = ("POST", "GET", "PUT", "DELETE", "PATCH")
    name = models.CharField(verbose_name="权限名称", max_length=50)
    is_menu = models.BooleanField("是否为菜单", default=True)  # ture就是菜单， false就是接口
    # 操作
    method = models.CharField(verbose_name="请求方式", max_length=8, default="", choices=method_choices)
    path = models.CharField(verbose_name="访问url地址", max_length=256, blank=True, null=True)
    desc = models.CharField(verbose_name="权限的描述", max_length=512, blank=True, null=True)
    menu = models.ForeignKey("MenuModel", null=True, blank=True, on_delete=models.CASCADE, related_name="permissions")

    # 权限与资源的关系
    # resource = models.ForeignKey("ResourceModel", on_delete=models.CASCADE, related_name="permissions")
    def __str__(self):
        return self.name

    class Meta:
        db_table = "t_permissions"
        verbose_name = "权限表"
        verbose_name_plural = verbose_name
        ordering = ["id"]


class RoleModel(BaseModel):
    """
    角色模型
    """
    name = models.CharField(verbose_name="角色名称", max_length=50, unique=True)
    permissions = models.ManyToManyField(PermissionsModel, verbose_name="角色的权限", db_table="t_roles_permissions",
                                         blank=True, null=True)
    desc = models.CharField(verbose_name="角色描述", max_length=512, blank=True, null=True)
    dept = models.ForeignKey("DeptModel", verbose_name="用户所在部门", blank=True, null=True, on_delete=models.SET_NULL)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "t_roles"
        verbose_name = "角色"
        verbose_name_plural = verbose_name
        ordering = ['id']
